#include <EEPROM.h>

//继电器控制端口
#define GPIO_ANT_1_1 5
#define GPIO_ANT_1_2 6
#define GPIO_ANT_2_1 7
#define GPIO_ANT_2_2 8

#define ConfigObjectSta 15

struct ConfigObject {
  byte AntIndex;//天线切换索引  
  byte SDataType;//协议类型
  byte WMode;//工作模式 1:手动 0:自动
  unsigned int AntFreq_1[2];//天线1自动切换频率
  unsigned int AntFreq_2[2];//天线2自动切换频率
};

class Config{
public:
  const String ComName="COM";//主入口名称
  const String AntName_1="ANT-A";
  const String AntName_2="ANT-B";//天线名称
  
  ConfigObject ConfigInfo={
    0,   
    0,
    0,
    0,
    0   
  };
  void GetConfig()
  {
    /*

    ComName=Read_String(0,5);

    if(ComName=="")
    {
      ComName="COM";   
    }
    
    AntName_1=Read_String(5,5);
    if(AntName_1=="" && AntName_1.length()<0)AntName_1="ANT-A";    
    
    AntName_2=Read_String(10,5);
    if(AntName_2=="" && AntName_2.length()<0)AntName_2="ANT-B";   
    */
    EEPROM.get(ConfigObjectSta, ConfigInfo);  
    ConfigInfo.AntFreq_1[0]=1;
    ConfigInfo.AntFreq_1[1]=50; 
    
    ConfigInfo.AntFreq_2[0]=100;
    ConfigInfo.AntFreq_2[1]=900; 
  }

  void SaveConfig(){
    EEPROM.put(ConfigObjectSta, ConfigInfo);
    //if(ConfigInfo.AntFreq_1[0]==0 || ConfigInfo.AntFreq_1[1]==0)
    //{
      ConfigInfo.AntFreq_1[0]=1;
      ConfigInfo.AntFreq_1[1]=50;
    //}
    
    //if(ConfigInfo.AntFreq_2[0]==0 || ConfigInfo.AntFreq_2[1]==0)
    //{
      ConfigInfo.AntFreq_2[0]=100;
      ConfigInfo.AntFreq_2[1]=900;
    //}
    Write_String(ComName,0,5);
    Write_String(AntName_1,5,5);
    Write_String(AntName_2,10,5);
    
  }  
    
  //a写入字符串长度，b是起始位，str为要保存的字符串
  void Write_String(String str,int staAdder, int len){
      
    byte StrLen=str.length();
    for (int i = 0; i < len; i++){
      if(StrLen<i)
      {
        EEPROM.write(staAdder + i,255);
      }
      else{
        EEPROM.write(staAdder + i, str[i]);
      }    
    }
    
  }
  
  //staAdder:起止位置 len:读取长度
  String Read_String(byte adder, byte len){ 
    String data = "";
    //从EEPROM中逐个取出每一位的值，并链接
    for (byte i = 0; i < len; i++){
      byte t=EEPROM.read(i+adder);
      if(t>=48 && t<=122)
      {      
        
        data += char(t);
      }   
    }
    return data;
  }

};

class ASwitch{
public:
  void begin(){
    pinMode(GPIO_ANT_1_1, OUTPUT);
    pinMode(GPIO_ANT_1_2, OUTPUT);
    pinMode(GPIO_ANT_2_1, OUTPUT);
    pinMode(GPIO_ANT_2_2, OUTPUT);

    SetAnt(255);
  }
  
  void SetAnt(byte antIndex)
  {
    switch(antIndex)
    {
      case 0:
        digitalWrite(GPIO_ANT_1_1, HIGH);
        digitalWrite(GPIO_ANT_1_2, HIGH);
        digitalWrite(GPIO_ANT_2_1, LOW);
        digitalWrite(GPIO_ANT_2_2, LOW);
      break;
      case 1:
        digitalWrite(GPIO_ANT_1_1, LOW);
        digitalWrite(GPIO_ANT_1_2, LOW);
        digitalWrite(GPIO_ANT_2_1, HIGH);
        digitalWrite(GPIO_ANT_2_2, HIGH);
      break;
      default:
        digitalWrite(GPIO_ANT_1_1, LOW);
        digitalWrite(GPIO_ANT_1_2, LOW);
        digitalWrite(GPIO_ANT_2_1, LOW);
        digitalWrite(GPIO_ANT_2_2, LOW);
      break;
    }
    
  }
  
};
